package com.loto.insurance.mis.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.loto.insurance.mis.web.db.mapper.OemInfoMapper;
import com.loto.insurance.mis.web.db.mapper.ProductInfoMapper;
import com.loto.insurance.mis.web.entity.OemInfo;
import com.loto.insurance.mis.web.entity.ProductInfo;
import com.loto.insurance.mis.web.entity.ajax.BaseJson;
import com.loto.insurance.mis.web.enumeration.IsActiveEnum;
import com.loto.utils.Http;
import com.loto.utils.XML;

@Service
public class ProductInfoService {

  static Logger logger = LogManager.getLogger(ProductInfoService.class);

  @Autowired
  ProductInfoMapper productInfoMapper;

  @Autowired
  OemInfoMapper oemInfoMapper;

  @Autowired
  DataSourceTransactionManager transactionManager;

  @Autowired
  private DefaultTransactionDefinition def;

  public ArrayList<ProductInfo> findAllProducts(Map<String, Object> params) {
	ArrayList<ProductInfo> productList = productInfoMapper
	    .findAllProductsPager(params);
	return productList;
  }

  public long findAllProductsCount(Map<String, Object> params) {
	return productInfoMapper.findAllProductsPagerCount(params);
  }

  public ProductInfo findProductInfoById(long productId) {
	return productInfoMapper.findProductInfoById(productId);
  }

  public boolean addProductInfo(ProductInfo productInfo) {
	boolean isOk = false;
	if (productInfoMapper.insertProduct(productInfo) > 0) {
	  com.loto.insurance.mis.web.entity.service.ProductInfo product = productInfoMapper
		  .findServiceProductInfoById(productInfo.getProductId());
	  if (product != null
		  && product.getProductIsActive() == IsActiveEnum.YES.getIndex()) {
		String xmlRequest = XML.toXML(product,
		    com.loto.insurance.mis.web.entity.service.ProductInfo.class);
		logger.info(xmlRequest);
		// 通知各OEM
		List<OemInfo> oemList = oemInfoMapper.findAllActiveOem();
		for (OemInfo oem : oemList) {
		  Map<String, String> paramMap = new HashMap<String, String>();
		  paramMap.put("content", xmlRequest);
		  paramMap.put("acc", oem.getOemAcc());
		  paramMap.put("pin", oem.getOemPin());
		  try {
			String xmlResponse = Http.doPost("http://" + oem.getOemDomain()
			    + "/webservice/createproduct.service", paramMap);
			logger.info(xmlResponse);
			BaseJson response = XML.fromXML(xmlResponse, BaseJson.class);
			if (response == null || !response.isOk()) {
			  logger.error("添加产品[" + product.getProductId() + "-"
				  + product.getProductName() + "]通知失败：" + oem.getOemName());
			} else
			  logger.info("添加产品[" + product.getProductId() + "-"
				  + product.getProductName() + "]通知成功：" + oem.getOemName());
		  } catch (Exception ex) {
			ex.printStackTrace();
			logger.error("添加产品[" + product.getProductId() + "-"
			    + product.getProductName() + "]通知失败：" + oem.getOemName());
		  }
		}
		isOk = true;
	  } else {
		logger.info("新建产品状态无效，无须通知各OEM");
		isOk = true;
	  }
	}
	return isOk;
  }

  public boolean updateProduct(ProductInfo productInfo) {
	boolean isOk = false;
	if (productInfoMapper.updateProduct(productInfo) > 0) {
	  com.loto.insurance.mis.web.entity.service.ProductInfo product = productInfoMapper
		  .findServiceProductInfoById(productInfo.getProductId());
	  if (product != null) {
		if (product.getProductIsActive() == IsActiveEnum.YES.getIndex()) {
		  String xmlRequest = XML.toXML(product,
			  com.loto.insurance.mis.web.entity.service.ProductInfo.class);
		  logger.info(xmlRequest);
		  // 通知各OEM
		  List<OemInfo> oemList = oemInfoMapper.findAllActiveOem();
		  for (OemInfo oem : oemList) {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("content", xmlRequest);
			paramMap.put("acc", oem.getOemAcc());
			paramMap.put("pin", oem.getOemPin());
			try {
			  String xmlResponse = Http.doPost("http://" + oem.getOemDomain()
				  + "/webservice/updateproduct.service", paramMap);
			  logger.info(xmlResponse);
			  BaseJson response = XML.fromXML(xmlResponse, BaseJson.class);
			  if (response == null || !response.isOk()) {
				logger.error("更新产品通知失败：" + oem.getOemName());
			  } else
				logger.info("更新产品[" + product.getProductId() + "-"
				    + product.getProductName() + "]通知成功：" + oem.getOemName());
			} catch (Exception ex) {
			  ex.printStackTrace();
			  logger.error("更新产品[" + product.getProductId() + "-"
				  + product.getProductName() + "]通知失败：" + oem.getOemName());
			}
		  }
		  isOk = true;
		} else {
		  // 通知各OEM
		  List<OemInfo> oemList = oemInfoMapper.findAllActiveOem();
		  for (OemInfo oem : oemList) {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("productId", String.valueOf(product.getProductId()));
			paramMap.put("acc", oem.getOemAcc());
			paramMap.put("pin", oem.getOemPin());
			try {
			  String xmlResponse = Http.doPost("http://" + oem.getOemDomain()
				  + "/webservice/removeproduct.service", paramMap);
			  logger.info(xmlResponse);
			  BaseJson response = XML.fromXML(xmlResponse, BaseJson.class);
			  if (response == null || !response.isOk()) {
				logger.error("删除产品[" + product.getProductId() + "-"
				    + product.getProductName() + "]通知失败：" + oem.getOemName());
			  } else
				logger.info("删除产品[" + product.getProductId() + "-"
				    + product.getProductName() + "]通知成功：" + oem.getOemName());
			} catch (Exception ex) {
			  ex.printStackTrace();
			  logger.error("删除产品[" + product.getProductId() + "-"
				  + product.getProductName() + "]通知失败：" + oem.getOemName());
			}
		  }
		  isOk = true;
		}
	  }
	}
	return isOk;
  }

  public boolean notifyProductInfo(String oemId) {
	boolean isOk = false;
	List<com.loto.insurance.mis.web.entity.service.ProductInfo> productList = productInfoMapper
	    .findAllActiveServiceProductInfo();
	if (productList != null) {
	  for (com.loto.insurance.mis.web.entity.service.ProductInfo product : productList) {
		String xmlRequest = XML.toXML(product,
		    com.loto.insurance.mis.web.entity.service.ProductInfo.class);
		logger.info(xmlRequest);
		// 通知各OEM
		List<OemInfo> oemList = oemInfoMapper.findAllActiveOem();
		for (OemInfo oem : oemList) {
		  if (oemId == null || oemId.equals(oem.getOemId())) {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("content", xmlRequest);
			paramMap.put("acc", oem.getOemAcc());
			paramMap.put("pin", oem.getOemPin());
			try {
			  String xmlResponse = Http.doPost("http://" + oem.getOemDomain()
				  + "/webservice/createproduct.service", paramMap);
			  logger.info(xmlResponse);
			  BaseJson response = XML.fromXML(xmlResponse, BaseJson.class);
			  if (response == null || !response.isOk()) {
				logger.error("添加产品[" + product.getProductId() + "-"
				    + product.getProductName() + "]通知失败：" + oem.getOemName());
			  } else
				logger.info("添加产品[" + product.getProductId() + "-"
				    + product.getProductName() + "]通知成功：" + oem.getOemName());
			} catch (Exception ex) {
			  ex.printStackTrace();
			  logger.error("添加产品[" + product.getProductId() + "-"
				  + product.getProductName() + "]通知失败：" + oem.getOemName());
			}
		  }
		}
	  }
	  isOk = true;
	}
	return isOk;
  }

}
